<?php
namespace Common\Model;

use Common\Provider\SmsProvider;
use Common\Util\CacheUtils;
/**
* 错误日志记录用户
* @date: 2018年3月19日 上午10:42:21
* @author: liuyang
*/
class ErrorLogModel  extends BaseModel { 
    
    const ERROR=1;
    const WARN=2;
    
    private function _sendErrorMsg($type){
        $cacheName = CacheUtils::SYS_ERROR_MSG;
        $cacheObj = CacheUtils::get($cacheName); 
        if($cacheObj == null){
            $content="系统有预警错误,[code],类型:".$type;
            SmsProvider::dispatch("18588886143", $content);
            //五分钟预警一次
            CacheUtils::set($cacheName, time(), 300);
        }
    }
    
     public function addLog($userId,$type,$contentData,$logLevel=2){ 
        if($logLevel==self::ERROR){
            $this->_sendErrorMsg($type);
        }
        $data = array();
        $data['user_id']=$userId;
        $data['type']=$type;
        $contentData['user_agent']=$_SERVER['HTTP_USER_AGENT'];
        $data['log']=json_encode($contentData);   
        return $this->add($data);
     }
      
    
}
	 